package fruit;

import impl.Fruit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 初始化水果实例保存在Map对象中
 */
public class FruitMap {

    private static final HashMap<String, Fruit> hashMap = new HashMap<>();

    public static Fruit getFruit(String name) {
        return hashMap.get(name);
    }

    public static void setFruitList(List<Fruit> fruitList) {
        for (Fruit fruit : fruitList) {
            String name = fruit.getName();
            if (name == null) continue;
            hashMap.put(name, fruit);
        }
    }

    public static void setFruit(String name, Fruit fruit) {
        hashMap.put(name, fruit);
    }

    public static void initFruitList(){
        FruitMap.setFruitList(new ArrayList<Fruit>() {{
            add(new Apple("apple"));
            add(new Strawberry("strawberry"));
        }});
    }
}
